<?php
define('IN_QY',true);
session_start();
require("include/common.inc.php");


$order_id = $_COOKIE['dindanid'];
$is_tax= $_POST['is_tax'];
//$yhqid= $_POST['yhqid']; 
	 //
   if($_POST['taxman'] ==""){
      $taxman=0;
   
   }else{
    
    $taxman= $_POST['taxman'];
     }
//
   if($_POST['taxconfig'] ==""){
 $taxconfig= 0;
   
   }else{
   
$taxconfig= $_POST['taxconfig'];
     }	 	 
$expresstri=$_POST['expresstri'];
$taxpri=$_POST['taxpri'];
$yhqmoney=$_POST['yhqmoney'];


if($taxpri==""){
 $taxpri= 0;	
}
//库存判断?
//税费计算
// $sql = "select sum(totprice+expresstri) as tot from tbl_taorder where uid='".$_COOKIE['userid']."' and is_pay=0";
// $query = mysql_query($sql);
// $rows = mysql_fetch_array($query);
// $temtot = $rows['tot'];
// if ($is_tax==1) {	
	// $taxpr = ($temtot-$yhqmoney)*0.17;
// }else{
	// $taxpr = 0;
// }

//完善订单表

//计算可用余额

$sql1 = "select sum(yuee+cash_back) as keyong from tbl_user where id = '".$_COOKIE['userid']."'";
$query1= mysql_query($sql1);
$row1 = mysql_fetch_array($query1);

$sql2 = "select money from tbl_tenred where uid = '".$_COOKIE['userid']."'";
$query2= mysql_query($sql2);
$row2 = mysql_fetch_array($query2);

$kymoney=$row1['keyong']+$row2['money'];





//先清空为0 
$sql1 = "update tbl_taorder set totprice=0 where uid='".$_COOKIE['userid']."' and is_pay=0";
$res=mysql_query($sql1);
//普通价格
$sqlfee = "select sum(gprice*gnum) as totfee from tbl_taorder where orderid='".$order_id."'";
$queryfee = mysql_query($sqlfee);
$rowfee5 = mysql_fetch_array($queryfee);
//VIP 价格
$sqlvip = "select sum(vip_good_price*gnum) as totvip from tbl_taorder where vip_good_price!=0.00 and orderid='".$order_id."'";
$queryvip = mysql_query($sqlvip);
$rowfee6 = mysql_fetch_array($queryvip);
//
$sqlvip7 = "select sum(gprice*gnum) as totvip7 from tbl_taorder where vip_good_price=0.00 and orderid='".$order_id."'";
$queryvip7 = mysql_query($sqlvip7);
$rowfee7 = mysql_fetch_array($queryvip7);

$gsccjvip=$rowfee7['totvip7'] + $rowfee6['totvip'];

if($_POST['is_vip']==0){
	
$totprice=$rowfee5['totfee'];
}else{
$totprice=$gsccjvip;	
}
if($expresstri==""){
	
$expresstri=0;
}

if($totprice<$kymoney){
	
$totprice=0.1;	
	
$jiashangyue=$kymoney-$totprice;	
	
	
}

if($totprice==$kymoney){
	
$totprice=0.1;	
	
$jiashangyue=0.1;	
	
	
}

if($totprice>$kymoney){
	

$totprice=$totprice-$kymoney;		
$jiashangyue=0.00;	
	
	
}


//获取订单最大id
$sql = "select max(id) as maxid,orderid,addtime from tbl_taorder where uid='".$_COOKIE['userid']."' and is_pay=0";
$query = mysql_query($sql);
$row = mysql_fetch_array($query);

$sql = "update tbl_taorder set is_tax='".$is_tax."' ,taxman='".$taxman."' ,taxconfig='".$taxconfig."' , taxpri='".$taxpr."',totprice='".$totprice."', expresstri='".$expresstri."'  where id=".$row['maxid'];
mysql_query($sql);


//清空可用余额
$sql4 = "update tbl_user set yuee='".$jiashangyue."', cash_back=0.00 where id='".$_COOKIE['userid']."'";
mysql_query($sql4);

$sql5 = "update tbl_tenred set money=0.00 where uid='".$_COOKIE['userid']."'";

mysql_query($sql5);


	if($kymoney!=0){
		
		$sql6="INSERT INTO tbl_yuehistory VALUES(
	0,
	'".$_COOKIE['userid']."',
	'".$kymoney."',
	'".$order_id."',
	'".date('Y-m-d H:i:s')."',
	'".$jiashangyue."'
	)";
    mysql_query($sql6);	
	
	}






//设置为待支付
$sql2 = "update tbl_taorder set is_pay=3 where uid='".$_COOKIE['userid']."' and is_pay=0";
$res=mysql_query($sql2);
//清空购物车
$sql3 = "update tbl_taocart set isorder=1 where userid='".$_COOKIE['userid']."' and isorder=0";
mysql_query($sql3);
//获取订单信息
$sqla = "select * from tbl_taorder where orderid='".$order_id."'";
$querya=mysql_query($sqla);
$rowa=mysql_fetch_array($querya);

$sqlb = "select * from tbl_taospp where id='".$rowa['goodid']."'";
$queryb=mysql_query($sqlb);
$rowb=mysql_fetch_array($queryb);

$sqld = "select * from tbl_address where userid='".$_COOKIE['userid']."' AND moren='1'";
$queryd=mysql_query($sqld);
$rowd=mysql_fetch_array($queryd);


	$fahuoaddress=$rowd['province'].$rowd['city'].$rowd['diqu'].$rowd['dizhi'];
	
    $sqlc = "insert into tbl_fh values (0,'".$order_id."','".$rowd['tell']."','".$rowd['name']."','".$fahuoaddress."','".$rowb['chtitle']."','".$rowd['youb']."','".$rowd['diqu']."')";
    mysql_query($sqlc);


//


if ($res) {
	$arr = array('error'=>0, "msg"=>'success');
echo json_encode($arr);
}